package com.xtaller.sdboot.core;

import com.xtaller.sdboot.utils.helper.StrHelper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * @author : Taller
 * @date : 2020/1/21 00:24
 * @Description : JSON返回结果
 */
public class R {
    /***
     * 状态码
     */
    private int code;
    /***
     * 消息内容
     */
    private String msg;
    /***
     * 返回结果数据
     */
    private Object data;

    /**
     * 默认成功，无返回数据
     */
    public R(){
    }
    /**
     * 默认成功，有返回数据、及附加提示信息
     */
    public R(Object data, String additionalMsg){
        this.code = Status.OK.code();
        this.msg = Status.OK.label();
        initMsg(additionalMsg);
        this.data = data;
    }
    public static ResponseEntity ok(Status status, Object object) {
        return new ResponseEntity(new R(status, object), HttpStatus.OK);
    }
    public static ResponseEntity ok(String message, Object object) {
        return new ResponseEntity(new R(object, message), HttpStatus.OK);
    }
    public static ResponseEntity ok(String message) {
        return new ResponseEntity(new R(Status.OK, message), HttpStatus.OK);
    }
    public static ResponseEntity ok(Object object) {
        return new ResponseEntity(new R(Status.OK, object), HttpStatus.OK);
    }
    /***
     * 非成功，指定状态
     * @param status
     */
    public R(Status status){
        this.code = status.code();
        this.msg = status.label();
        initMsg(null);
        this.data = null;
    }

    /***
     * 非成功，指定状态及附加提示信息
     * @param status
     * @param additionalMsg
     */
    public R(Status status, String additionalMsg){
        this.code = status.code();
        this.msg = status.label();
        initMsg(additionalMsg);
        this.data = null;
    }

    /**
     * 非成功，指定状态、返回数据
     * @param status
     * @param data
     */
    public R(Status status, Object data){
        this.code = status.code();
        this.msg = status.label();
        initMsg(null);
        this.data = data;
    }

    /**
     * 非成功，指定状态、返回数据、及附加提示信息
     */
    public R(Status status, Object data, String additionalMsg){
        this.code = status.code();
        this.msg = status.label();
        initMsg(additionalMsg);
        this.data = data;
    }



    /***
     * 自定义JsonResult
     * @param code
     * @param label
     * @param data
     */
    public R(int code, String label, Object data){
        this.code = code;
        this.msg = label;
        this.data = data;
    }
    public static ResponseEntity error(Status status, String message, Object object) {
        return new ResponseEntity(new R(status, object, message), HttpStatus.valueOf(status.code()));
    }
    public static ResponseEntity error(Status status, String message) {
        return new ResponseEntity(new R(status, message), HttpStatus.valueOf(status.code()));
    }
    public static ResponseEntity error(Status status) {
        return new ResponseEntity(new R(status), HttpStatus.valueOf(status.code()));
    }

    public int getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
    public Object getData() {
        return data;
    }

    /**
     * 赋值msg（去掉重复前缀以支持与BusinessException嵌套使用）
     * @param additionalMsg
     */
    private void initMsg(String additionalMsg){
        try {
            if (additionalMsg != null || !additionalMsg.equals("")) {
                if (StrHelper.startsWith(additionalMsg, this.msg)) {
                    this.msg = additionalMsg;
                } else {
                    this.msg += ": " + additionalMsg;
                }
            }
        }catch (Exception ex){

        }
    }



}
